#include <stdio.h>
#define MAXLINE 1000    /* maximum input line size */

int getline(char s[], int lim);
void reverse(char s[]);

/* to avoid problems with getline (from the GNU C Library),
   compile using the -std=c99 flag */

/* reverse each input line */
int main()
{
    int len;            /* current line length */
    char line[MAXLINE];     /* current input line */
    
    while((len = getline(line, MAXLINE)) > 0){
	reverse(line);
	printf("%s", line);
    }
    
    return 0;
}

/* getline:  read a line into s, return length */
int getline(char s[], int lim)
{
    int c, i;

    for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
	s[i] = c;
    if(c == '\n'){
	s[i] = c;
	++i;
    }
    s[i] = '\0';
    return i;
}

/* reverse string; keep \0 and newline (if present) in place */
void reverse(char s[])
{
    int i, lim=0, c;

    while(s[lim] != '\0' && s[lim] != '\n')
	++lim;

    for(i=0; i<lim; ++i, --lim){
	c = s[i];
	s[i] = s[lim];
	s[lim] = c;
    }
}
